//
// Created by benxb on 2022/3/14.
//

#include <storage/data_chunk_pool.h>

namespace benxdb {

std::shared_ptr<DataChunkPool> DataChunkPool::instance_ = nullptr;

std::shared_ptr<DataChunkPool> DataChunkPool::GetPool() {
    if (instance_ == nullptr) {
        instance_.reset(new DataChunkPool());
    }
    return std::shared_ptr<DataChunkPool>();
}

void DataChunkPool::UnPin(DataChunk *data_chunk) {
    free_chunk_list_.push_back(data_chunk);
}

DataChunk *DataChunkPool::GetChunk() {
    DataChunk* data_chunk = nullptr;
    if (free_chunk_list_.empty()) {
        LOG_ERROR("No free chunk.");
    } else {
        data_chunk = free_chunk_list_.front();
        free_chunk_list_.pop_front();
    }
    return data_chunk;
}

DataChunkPool::DataChunkPool() {
    // init free_chunk_list
    for (auto & i : chunks_) {
        free_chunk_list_.push_back(&i);
    }
};

}